package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccountInfoActivity_L extends BaseActivity {

    @BindView(R.id.account)
    TextView account;
    @BindView(R.id.et_phone)
    EditText etPhone;
    String info;
    String id;
    String account_atring,phone_string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_info__l);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        info = intent.getStringExtra("info");
        id = intent.getStringExtra("id");
        if (info.equals("post")) {
            if (MyApp.account != null&&MyApp.account.length()>8) {
                account.setText(MyApp.account.substring(0, 3) + "****" + MyApp.account.substring(7));
            }
            if (MyApp.phone != null) {
                etPhone.setText(MyApp.phone);
            }
        } else {
            LoadingDialog.show(this);
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(this, "token", "") + "")
                    .addParams("id", id)
                    .url(API.BaseApi_L + API.GetHouseAccountInfo_Api)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            LoadingDialog.dismiss(AccountInfoActivity_L.this);
                            T.showShort(AccountInfoActivity_L.this, "数据加载失败，请稍后再试");
                        }

                        @Override
                        public void onResponse(String response) {
                            LoadingDialog.dismiss(AccountInfoActivity_L.this);
                            try {
                                JSONObject jsonObject=new JSONObject(response);
                                if (jsonObject.getInt("status")==0){
                                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                                    JSONObject jsonObject1=jsonArray.getJSONObject(0);
                                    if (jsonObject1.getString("hReceivables")!=null&&(jsonObject1.getString("hReceivables").length()>8)){
                                        account.setText(jsonObject1.getString("hReceivables").substring(0,3)+"****"+jsonObject1.getString("hReceivables").substring(7));
                                    }
                                    etPhone.setText(jsonObject1.getString("hPhone"));
                                    account_atring=jsonObject1.getString("hReceivables");
                                    phone_string=jsonObject1.getString("hPhone");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x001 && data != null) {
            if (resultCode == 0x002) {
                account_atring=data.getStringExtra("account");
                if (data.getStringExtra("account")!=null&&(data.getStringExtra("account").length()>9)){
                    account.setText(data.getStringExtra("account").substring(0, 4) + "****" + data.getStringExtra("account").substring(8));
                }
                MyApp.account = data.getStringExtra("account");
            }
        }
    }

    @OnClick({R.id.back, R.id.price_day, R.id.baocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.price_day:
                Intent intent = new Intent(this, SetAliAccountActivity.class);
                startActivityForResult(intent, 0x001);
                break;
            case R.id.baocun:
                if (info.equals("post")){
                Intent intent2 = new Intent();
                if (MyApp.account != null && !(etPhone.getText() + "").equals("")) {
                    MyApp.phone = etPhone.getText() + "";
                    intent2.putExtra("info", "完成");
                    setResult(0x006, intent2);
                    finish();
                } else {
                    T.showShort(this, "请完善资料");
                }
            }else {
                    phone_string=etPhone.getText()+"";
                    LoadingDialog.show(this);
                    Log.e("aaaaaaaaaaaaa","re="+SPUtils.get(this,"token","")+"");
                    Log.e("aaaaaaaaaaaaa","re="+id);
                    Log.e("aaaaaaaaaaaaa","re="+account_atring);
                    Log.e("aaaaaaaaaaaaa","re="+phone_string);
                    OkHttpUtils.get()
                            .addParams("token",SPUtils.get(this,"token","")+"")
                            .addParams("id",id)
                            .addParams("hReceivables",account_atring)
                            .addParams("hPhone",phone_string)
                            .url(API.BaseApi_L+API.UpdateHouseAccountInfo_Api)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    LoadingDialog.dismiss(AccountInfoActivity_L.this);
                                    T.showShort(AccountInfoActivity_L.this,"数据加载失败，请稍后再试");

                                }

                                @Override
                                public void onResponse(String response) {
                                    LoadingDialog.dismiss(AccountInfoActivity_L.this);
                                    try {
                                        JSONObject jsonObject=new JSONObject(response);
                                        if (jsonObject.getInt("status")==0){
                                            T.showShort(AccountInfoActivity_L.this,jsonObject.getString("msg") + " 后台审核中...");
                                            finish();
                                        } else {
                                            T.showShort(AccountInfoActivity_L.this,jsonObject.getString("msg"));
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            });


                }

                break;
        }
    }
}
